package adventure.commands;

import adventure.*;
import player.*;
import adventure.placeable.Item;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class Unlock extends AbstractCommand {

    public Unlock(Adventure a) {
        super(a, new String[]{"UNLOCK"});
    }

    @Override
    public String validateAndExecute(Player p, String[] args) {
        if (args.length == 1) {
            return "Unlock where?\nSyntax: unlock <direction>";
        } else if (args.length == 2 && Util.isDirection(args[1])) {
            return unlock(p, args[1].charAt(0));
        } else {
            return "That is not a valid direction!";
        }
    }

    private String unlock(Player player, char dir) {
        int x = player.x();
        int y = player.y();
        int z = player.z();

        if (!rooms.isBlocked(x, y, z, dir)) {
            if (rooms.isClosed(x, y, z, dir)) {
                if (rooms.isLocked(x, y, z, dir)) {
                    Item key = player.getKey(x, y, z, dir);
                    if (key.getID() > 0) {
                        // Unlock the door
                        rooms.unlock(x, y, z, dir);
                        String output = "You unlock the "
                                + ColourUtil.colourise(Util.fillDir(dir), ColourUtil.red)
                                + " door.";

                        out.broadcastLocalToPlayer(player,
                                ColourUtil.colourise(player.getName(), ColourUtil.magenta) + " unlocks the "
                                + ColourUtil.colourise(Util.fillDir(dir), ColourUtil.red)
                                + " door.");

                        output += "\n" + open(player, dir);

                        // Use key once
                        String keyName = key.getName();
                        boolean keyDestroyed = player.useItem(key.getID());

                        if (keyDestroyed) {
                            output += "\n"
                                    + ColourUtil.colourise(keyName, ColourUtil.cyan)
                                    + " crumbles into dust.";
                        }

                        return output;

                    } else {
                        return "You don't have the right key for that!";
                    }
                } else {
                    return "That door is not locked!";
                }
            } else {
                return "That way isn't closed!";
            }
        } else {
            return "That is not a valid direction!";
        }
    }

    private String open(Player player, char dir) {
        int x = player.x();
        int y = player.y();
        int z = player.z();

        if (!rooms.isBlocked(x, y, z, dir)) {
            if (!rooms.isLocked(x, y, z, dir)) {
                if (rooms.isClosed(x, y, z, dir)) {
                    rooms.open(x, y, z, dir);
                    out.broadcastLocalToPlayer(player,
                            ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                            + " opens the "
                            + ColourUtil.colourise(Util.fillDir(dir), ColourUtil.yellow)
                            + " door.");

                    return "You open the "
                            + ColourUtil.colourise(Util.fillDir(dir), ColourUtil.yellow)
                            + " door.";
                } else {
                    return "That way isn't closed!";
                }
            } else {
                return "That door is locked!";
            }
        } else {
            return "That is not a valid direction!";
        }
    }
}
